﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QueryMBOMKnife : BasePage
    {
        int m_count = 0;
        public string m_strMainKey = "KnifeID";
        public DataTable BOMKnifeDT
        {
            get
            {
                if (Session["BOMKnifeDT"] == null)
                {
                    return CreateKnifeDt();
                }
                else
                {
                    return (DataTable)Session["BOMKnifeDT"];
                }
            }
            set
            {
                Session["BOMKnifeDT"] = value;
            }
        }
        public DataTable CreateKnifeDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BOMKnifeID");
            dt.Columns.Add("KnifeID");//刀具编号
            dt.Columns.Add("KnifeName");//刀具名称
            dt.Columns.Add("Specifications");//规格
            dt.Columns.Add("Nums");//数量
            dt.Columns.Add("KnifeStatus");//刀具状态
            dt.Columns.Add("IsNew");
            dt.Columns.Add("IsDel");
            return dt;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData(0);
            }
        }


        private void BindData(int pageIndex)
        {
            DataTable dt = BsBOMBusiness.SelectDialogKnife(txtKnifeID.Text.Trim(), txtKnifeName.Text.Trim(), pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                DataView dv = new DataView(BOMKnifeDT);
                dv.RowFilter = "IsDel<>'1'";
                DataTable dtTemp = dv.ToTable();

                EnableGridViewRow(grdResult, dtTemp, m_strMainKey, false);
                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        private void AddPro()
        {
            BOMKnifeDT = GetAddPro(BOMKnifeDT).Copy();
        }

        private DataTable GetAddPro(DataTable dt)
        {
            DataTable dtTemp = dt.Copy();
            foreach (GridViewRow gvr in grdResult.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    Label lblKnifeID = gvr.FindControl("lblKnifeID") as Label;
                    Label lblKnifeName = gvr.FindControl("lblKnifeName") as Label;
                    Label lblSpecifications = gvr.FindControl("lblSpecifications") as Label;

                    if (((CheckBox)gvr.FindControl("cbChoose")).Checked && gvr.Enabled)
                    {
                        DataRow[] drs = dtTemp.Select("KnifeID='" + lblKnifeID.Text.Trim() + "'");
                        if (drs.Length == 0)
                        {
                            DataRow dr = dtTemp.NewRow();
                            dr["KnifeID"] = lblKnifeID.Text.Trim();
                            dr["KnifeName"] = lblKnifeName.Text.Trim();
                            dr["Specifications"] = lblSpecifications.Text.Trim();
                            dr["IsDel"] = "-1";
                            dr["IsNew"] = "1";
                            dtTemp.Rows.Add(dr);
                        }
                        else if (drs.Length > 0 && CommonMethod.GetString(drs[0]["IsDel"]) == "1")
                        {
                            drs[0]["KnifeID"] = lblKnifeID.Text.Trim();
                            drs[0]["KnifeName"] = lblKnifeName.Text.Trim();
                            drs[0]["Specifications"] = lblSpecifications.Text.Trim();
                            drs[0]["IsDel"] = "-1";
                        }
                    }
                    else
                    {
                        DataRow[] drs = dtTemp.Select("KnifeID='" + lblKnifeID.Text.Trim() + "'");
                        if (drs.Length > 0)
                        {
                            drs[0]["IsDel"] = "1";
                        }
                    }
                }
            }
            return dtTemp;
        }
        protected void btnOk_Click(object sender, EventArgs e)
        {
            AddPro();
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'success');CloseTopDialog();</script>");
        }

        protected void pcData_PageIndexClick(object sender, Controls.PageIndexClickEventArgs e)
        {
            AddPro();
            BindData(e.PageIndex);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AddPro();
            BindData(0);
        }
    }
}